package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);
        // 保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据套餐id获取套餐和套餐内菜品信息
     *
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long setmealId) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(setmealId);
        BeanUtils.copyProperties(setmeal, setmealDto);

        List<SetmealDish> setmealDishes = setmealDishService.getBySetmealId(setmealId);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    /**
     * 修改套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        // 1.修改套餐基础信息
        this.updateById(setmealDto);
        // 2.修改套餐内菜品信息
        // 2.1删除套餐id对应的菜品的信息
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmealDto.getId()));
        // 2.2插入套餐内菜品的信息
        // 2.2.1先获取套餐内菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 2.2.2遍历集合，设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        // 2.2.3批量插入
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Override
    public void removeWithDish(List<Long> ids) {
        List<Setmeal> setmealList = this.list(new LambdaQueryWrapper<Setmeal>().in(Setmeal::getId, ids));
        // 判断是否有在售的套餐
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus() == 1) {
                throw new CustomException("套餐正在售卖中，不能删除");
            }
        }
        // 删除套餐
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId, ids));
        this.removeByIds(ids);
    }
}
